﻿using DataProvider;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Model;
using Newtonsoft.Json;
using Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Utility;

namespace api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Sys_ModuleController : ControllerBase
    {
        public IActionResult GetTreeList(LayPageModel param)
        {
            var List = Sys_ModuleProvider.Instances.GetTreeList();
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, count = List.Count, data = List })) ;
        }
        public IActionResult Save(CommonSaveModel param)
        {
            var userToken = (UserCache)JwtHelp.GetToken(HttpContext.Request.Headers["Authorization"].ToString()).data; ;
            var Ent= JsonConvert.DeserializeObject<Sys_Module>(param.strEntity);
            if(Ent.Id==0)
            {
                Ent.CreateUser = userToken.UserId;
                Ent.CreateTime = DateTime.Now;
                Sys_ModuleService.Instances.Add(userToken.UserId, Ent);
            }
            else
            {
                Ent.LastUpdateTime = DateTime.Now;
                Ent.LastUpdateUser = userToken.UserId;
                Sys_ModuleService.Instances.Edit(userToken.UserId, Ent);
            }
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, data="Success" }));
        }
        public IActionResult Del(CommonIds param)
        {
            var userToken = (UserCache)JwtHelp.GetToken(HttpContext.Request.Headers["Authorization"].ToString()).data; ;
            var List = Sys_ModuleService.Instances.GetListByIds(param.Ids);
            foreach(var d in List)
            {
                d.LastUpdateTime = DateTime.Now;
                d.LastUpdateUser = userToken.UserId;
                d.Status = Status.delete;
              
            }
            Sys_ModuleService.Instances.EditBatch(userToken.UserId, List);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0, data = "Delete Success" }));
        }
        public IActionResult GetModuleListByUser()
        {
            var userToken = JwtHelp.GetToken(HttpContext.Request.Headers["Authorization"].ToString()); //获取token信息
            if (userToken.code > 0)
                return Ok(JsonConvert.SerializeObject(userToken));

            var userCache = (UserCache)userToken.data;

            if(userCache.IsSuperAdmin)
            {
                var ModuleList = Sys_ModuleService.Instances.QueryCustom(" Status=0 ");

                List<dynamic> dataList = new List<dynamic>();
                var oneClassModule = ModuleList.Where(x => x.ParentId == 0).OrderBy(x => x.OrderNumber).ToList();
                foreach (var item in oneClassModule)
                {
                    
                    dataList.Add(new
                    {
                        name = item.ModuleName,
                        icon = item.ModuleIcon,
                        url = string.IsNullOrEmpty(item.ModuleUrl) ? "javascript:;" : item.ModuleUrl,
                        menuid = item.Id,
                        navtitle = item.ModuleName,
                        subMenus = ErgodicMenuChina(ModuleList, item.Id, item.ModuleName, item.Id),
                        topparentid = item.Id,
                    });
                }
                return Ok(JsonConvert.SerializeObject(new ResultData<object>() { count = dataList.Count, data = dataList }));
            }
            return null;
        }
        private List<dynamic> ErgodicMenuChina(List<Sys_Module> moduleList, int moduleId, string parentName, int topId)
        {
            var chindList = moduleList.Where(x => x.ParentId == moduleId).OrderBy(x => x.OrderNumber).ToList();
            if (chindList.Count == 0)
                return null;
            List<dynamic> dataList = new List<dynamic>();
            foreach (var item in chindList)
            {
                //if(item.Name=="数据源配置"||item.Name=="数据字典")
                //{
                //    Console.WriteLine("123456");
                //}
                dataList.Add(new
                {
                    name = item.ModuleName,
                    icon = item.ModuleIcon,
                    url = string.IsNullOrEmpty(item.ModuleUrl) ? "javascript:;" : item.ModuleUrl,
                    menuid = item.Id,
                    navtitle = parentName,
                    subMenus = ErgodicMenuChina(moduleList, item.Id, parentName, topId),
                    parentid = item.ParentId,
                    topparentid = topId,
                });
            }
            return dataList;
        }
    }
}
